home *** CD-ROM | disk | FTP | other *** search
- Path: canberra.DIALix.oz.au!not-for-mail
- From: fruitbat@canberra.DIALix.oz.au (Paul Sleigh)
- Newsgroups: comp.lang.c++
- Subject: Idle curiosity: Delphi properties in C++
- Date: 7 Jan 1996 01:38:49 +1100
- Organization: DIALix Services, Canberra, Australia.
- Sender: fruitbat@canberra.DIALix.oz.au
- Message-ID: <4cm1hp$mok$1@canberra.DIALix.oz.au>
- NNTP-Posting-Host: fruitbat@canberra.dialix.oz.au
-
- I freely admit I'm a Delphi programmer, but I also like playing with any
- language I can find, and thinking about them (what can I say -- my
- girlfriend's away for the weekend). So here's a puzzle for those more
- knowledgeable than I:
-
- One major advantage Delphi has over Borland Pascal 7.0, Visual Basic and
- (apparently) C++ is called a property. If you'll excuse a breath of Pascal
- in this holy of holies C++ group, I'll demonstrate the use of this feature:
-
- type
- TDate = record Year, Month, Day: Integer; end;
- TPerson = class
- private
- FBirthday: TDate;
- procedure SetBirthday(ABirthday: TDate);
- function GetAge: Integer;
- public
- property Birthday: TDate read FBirthday write SetBirthday;
- property Age: Integer read GetAge;
- end;
-
- For the benefit of any of you who never had the good fortune to learn
- Pascal, I think the simplest C++ equivalent would be as listed at the end of
- this article.
-
- Basically, how it works is that, given the following definitions:
-
- var
- P: TPerson;
- A: Integer;
- D: TDate;
-
- you can do the following, with the actual results as listed in comments:
-
- P.Birthday := D; (* exactly like P.SetBirthday(D); *)
- WriteLn(P.Birthday.Year); (* WriteLn(P.FBirthday.Year); *)
- WriteLn('I am ',P.Age,' years old!'); (* WriteLn(... P.GetAge ...); *)
-
- So the question is: how, using macros or templates or whatever, would you
- gain the elegance and simplicity of this feature using Standard C++? Note
- that I'm not debating whether you _need_ it or not; obviously we survived
- for years without it and could go on a lot longer quite happily, but as I
- said, I'm curious.
-
- Here, for the benfit of philist^H^H^H^Hnon-Pascal-programmers, is the type
- definition in what I think is standard C -- except for the property parts,
- which I'll fudge:
-
- struct TDate
- { int Year, Month, Day; };
-
- class TPerson
- {
- private:
- TDate FBirthday;
- void SetBirthday(TDate ABirthday);
- int GetAge();
- public:
- property TDate Birthday read FBirthday write SetBirthday; // ???
- property int Age read GetAge; // ???
- };
- --
- Let's just say that if complete and utter chaos was light- | Paul Sleigh
- ning, he'd be the sort to stand on a hilltop in a thunder- | Eric the Fruitbat
- storm wearing wet copper armour and shouting 'All gods are | fruitbat@canberra
- bastards'. [T Pratchett: Rincewind discussing Twoflower] | .DIALix.oz.au
-